<template>
	<view>
		<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="num"
			:accept="accept" :previewFullImage="true" width="80" height="80">
			<image src="@/static/img/upload.png" mode="widthFix" style="width: 160rpx;height: 160rpx;"></image>
		</u-upload>
	</view>
</template>

<script>
	export default {
		name: "hUpload",
		props: {
			defaultImage: {
				type: Array,
				default: []
			},
			num: {
				type: Number,
				default: 9
			},
			accept: {
				type: String,
				default: 'image'
			},
			lang: {
				type: String,
				default: 'en'
			}
		},
		data() {
			return {
				fileList1: [],
				list: []
			};
		},
		mounted() {
			setTimeout(() => {
				if (this.$props.defaultImage.length) {
					let list = []
					this.$props.defaultImage.forEach(item => {
						list.push({
							url: item
						})
					})
					this.fileList1 = [...list]
				}
			}, 300)
		},
		methods: {
			uploadInit() {
				this.fileList1 = []
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.$emit('delImage', event.index)
			},
			// 新增图片
			async afterRead(event) {
				console.log(event);
				console.log(this[`fileList${event.name}`]);
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: this.lang == 'en' ? 'Uploading' : '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.helper.uploadImage('https://www.slem.com.au/api/common/upload',
						lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
					this.list.push(result)
				}
				this.$emit('sendImage', {
					sendList: this.list,
					fileListLen
				})
			},
		}
	}
</script>